﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SlidingMessageControl
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:SlidingMessage runat=server></{0}:SlidingMessage>")]
    public class SlidingMessage : WebControl
    {
        public SlidingMessage()
        {
            this.Visible = false;
            LoadDefaultOptions();
        }

        #region Control Properties
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        /// <summary>
        /// The message that should be displayed
        /// </summary>
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? "[" + this.ID + "]" : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue(OptionEnumerations.MessagePosition.top)]
        /// <summary>
        /// The position of the message box
        /// </summary>
        public OptionEnumerations.MessagePosition Position
        {
            get
            {
                OptionEnumerations.MessagePosition _posn = (OptionEnumerations.MessagePosition)ViewState["Position"];
                return _posn;
            }
            set
            {
                ViewState["Position"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue(50)]
        /// <summary>
        /// The value that will be used for the line-height of the box
        /// </summary>
        public int Size
        {
            get
            {
                int _size = Convert.ToInt32(ViewState["BoxSize"]);
                return _size;
            }
            set
            {
                ViewState["BoxSize"] = value;
            }
        }

        public double FontSize
        {
            get
            {
                return Convert.ToDouble(ViewState["FontSize"]);
            }
            set
            {
                ViewState["FontSize"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue(1500)]
        public int Delay
        {
            get
            {
                int _delay = Convert.ToInt32(ViewState["Delay"]);
                return _delay;
            }
            set
            {
                ViewState["Delay"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue(500)]
        public int Speed
        {
            get
            {
                int _speed = Convert.ToInt32(ViewState["Speed"]);
                return _speed;
            }
            set
            {
                ViewState["Speed"] = value;
            }
        }

        [Bindable(true)]
        [Category("Resource")]
        [DefaultValue(true)]
        public bool IncludeJquery
        {
            get
            {
                bool _jquery = Convert.ToBoolean(ViewState["JqueryLagbey"]);
                return _jquery;
            }
            set
            {
                ViewState["JqueryLagbey"] = value;
            }
        }
        #endregion

        public void Show()
        {
            this.Visible = true;
        }

        public void Hide()
        {
            this.Visible = false;
        }

        protected override void OnPreRender(EventArgs e)
        {
            string jquerySource = "SlidingMessageControl.Resources.jquery_1_3_2_min.js";
            string jquerySliderSource = "SlidingMessageControl.Resources.jquery_slidingmessage_min.js";

            // Get a ClientScriptManager reference from the Page class.
            ClientScriptManager cs = Page.ClientScript;

            // Register the client resources with the page.
            if(IncludeJquery) cs.RegisterClientScriptResource(typeof(SlidingMessage), jquerySource);
            cs.RegisterClientScriptResource(typeof(SlidingMessage), jquerySliderSource);

            if (this.Visible)
            {
                string id = this.ClientID + "_box";
                string position = Enum.GetName(typeof(OptionEnumerations.MessagePosition), Position);
                string background = System.Drawing.ColorTranslator.ToHtml(this.BackColor);
                string fontSize = this.FontSize.ToString() + "px";
                string cssClass = String.IsNullOrEmpty(this.CssClass) ? id + "_class" : this.CssClass;

                string optionsScript = String.Format(@"var {0}_options = {{
                                                       id: '{0}',
                                                       position: '{1}',
                                                       size: {2},
                                                       backgroundColor: '{3}',
                                                       delay: {4},
                                                       speed: {5},
                                                       fontSize: '{6}',
                                                       class: '{7}'
                                                     }};", id, position, this.Size, background, this.Delay, this.Speed, fontSize, this.CssClass);

                string showScript = String.Format("$.showMessage('{0}', {1}_options);", this.Text, id);

                StringBuilder slidingScript = new StringBuilder();
                slidingScript.AppendLine(optionsScript);
                slidingScript.AppendLine(showScript);

                cs.RegisterClientScriptBlock(typeof(SlidingMessage), "_SLIDINGSCRIPT", slidingScript.ToString(), true);
            }

            // Restore back to default state
            LoadDefaultOptions();
            
            base.OnPreRender(e);
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            
        }

        private void LoadDefaultOptions()
        {
            // Initialize with default values
            this.IncludeJquery = true;
            this.FontSize = 30;
            this.BackColor = System.Drawing.ColorTranslator.FromHtml("#D9F0FC");
            this.Position = OptionEnumerations.MessagePosition.top;
            this.Size = 50;
            this.Speed = 500;
            this.Delay = 1500;
            this.CssClass = "";
        }
    }
}
